package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.TestKeysBean;

/**
 * Created by chenchangxing on 15/12/29.
 */
public class CheckAnswerFragment extends Fragment {
    @Bind(R.id.check_answer_type)
    TextView checkAnswerType;
    @Bind(R.id.check_answer_title)
    TextView checkAnswerTitle;
    @Bind(R.id.check_answer_select_icon_a)
    ImageView checkAnswerSelectIconA;
    @Bind(R.id.check_answer_select_answer_a)
    TextView checkAnswerSelectAnswerA;
    @Bind(R.id.check_answer_select_a)
    LinearLayout checkAnswerSelectA;
    @Bind(R.id.check_answer_select_icon_b)
    ImageView checkAnswerSelectIconB;
    @Bind(R.id.check_answer_select_answer_b)
    TextView checkAnswerSelectAnswerB;
    @Bind(R.id.check_answer_select_b)
    LinearLayout checkAnswerSelectB;
    @Bind(R.id.check_answer_select_icon_c)
    ImageView checkAnswerSelectIconC;
    @Bind(R.id.check_answer_select_answer_c)
    TextView checkAnswerSelectAnswerC;
    @Bind(R.id.check_answer_select_c)
    LinearLayout checkAnswerSelectC;
    @Bind(R.id.check_answer_select_icon_d)
    ImageView checkAnswerSelectIconD;
    @Bind(R.id.check_answer_select_answer_d)
    TextView checkAnswerSelectAnswerD;
    @Bind(R.id.check_answer_select_d)
    LinearLayout checkAnswerSelectD;
    @Bind(R.id.check_answer_select)
    LinearLayout checkAnswerSelect;
    @Bind(R.id.check_answer_judge_icon_a)
    ImageView checkAnswerJudgeIconA;
    @Bind(R.id.check_answer_judge_answer_a)
    TextView checkAnswerJudgeAnswerA;
    @Bind(R.id.check_answer_judge_a)
    LinearLayout checkAnswerJudgeA;
    @Bind(R.id.check_answer_judge_icon_b)
    ImageView checkAnswerJudgeIconB;
    @Bind(R.id.check_answer_judge_answer_b)
    TextView checkAnswerJudgeAnswerB;
    @Bind(R.id.check_answer_judge_b)
    LinearLayout checkAnswerJudgeB;
    @Bind(R.id.check_answer_judge)
    LinearLayout checkAnswerJudge;

    List<TestKeysBean.DataEntity> dataEntities;
    int position;
    String tid;
    String ttype;
    @Bind(R.id.check_answer_detail)
    TextView checkAnswerDetail;

    public CheckAnswerFragment(List<TestKeysBean.DataEntity> dataEntities, int position) {
        this.dataEntities = dataEntities;
        this.position = position;
        tid = dataEntities.get(position).getId();
        ttype = dataEntities.get(position).getTtype();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_check_answer, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        initInfo();
    }

    private TestKeysBean.DataEntity getItem() {
        return dataEntities.get(position);
    }

    private void initInfo() {
        try {
        if (TextUtils.isEmpty(dataEntities.get(position).getExplain())) {
            checkAnswerDetail.setText("无");
        } else {
            checkAnswerDetail.setText(dataEntities.get(position).getExplain());
        }

        checkAnswerTitle.setText(getItem().getTitle());
        int type = Integer.parseInt(getItem().getTtype());
        int right = Integer.parseInt(getItem().getRight());
        int choose = Integer.parseInt(getItem().getChoose());
        switch (type) {

            case 1: {
                checkAnswerType.setText("选");
                checkAnswerSelect.setVisibility(View.VISIBLE);
                checkAnswerJudge.setVisibility(View.GONE);
                checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                if (right == choose) {
                    switch (right) {
                        case 1: {
                            checkAnswerSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 2: {
                            checkAnswerSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 3: {
                            checkAnswerSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 4: {
                            checkAnswerSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                    }
                } else {
                    switch (right) {
                        case 1: {
                            initAnswer(checkAnswerSelectIconA);
                            break;
                        }
                        case 2: {
                            initAnswer(checkAnswerSelectIconB);
                            break;
                        }
                        case 3: {
                            initAnswer(checkAnswerSelectIconC);
                            break;
                        }
                        case 4: {
                            initAnswer(checkAnswerSelectIconD);
                            break;
                        }
                    }
                }
                break;
            }
            case 2: {
                checkAnswerType.setText("判");
                checkAnswerJudge.setVisibility(View.VISIBLE);
                checkAnswerSelect.setVisibility(View.GONE);
                if (choose == right) {
                    switch (right) {
                        case 1: {
                            checkAnswerJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 2: {
                            checkAnswerJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                    }
                } else {
                    switch (right) {
                        case 1: {
                            initJudgeAnswer(checkAnswerJudgeIconA);
                            break;
                        }
                        case 2: {
                            initJudgeAnswer(checkAnswerJudgeIconB);
                            break;
                        }
                    }
                }
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initAnswer(ImageView icon) {
        checkAnswerSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);

        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    private void initJudgeAnswer(ImageView icon) {
        checkAnswerJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);

        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public String getTid() {
        return tid;
    }

    public String getTtype() {
        return ttype;
    }
}
